﻿using MediatR;
using Medical.Api.Write.Application.Command.RecyclingCleaning;
using Medical.Domain.RecoveryCleaning;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;
using Medical.Domain.Enum;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;

namespace Medical.Api.Write.Application.Handler.RecyclingCleaning
{
    /// <summary>
    /// 修改登记回收状态命令处理
    /// </summary>
    public class UpdateRecoveryStatusCommandHandlter : IRequestHandler<UpdateRecoveryStatusCommand, ApiResult<int>>
    {
        /// <summary>
        /// 登记回收仓储
        /// </summary>
        private readonly IBaseRepository<InstrumentRecovery> instrumentRecoveryRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="instrumentRecoveryRep">登记回收仓储</param>
        public UpdateRecoveryStatusCommandHandlter(IBaseRepository<InstrumentRecovery> instrumentRecoveryRep)
        {
            this.instrumentRecoveryRep = instrumentRecoveryRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>

        public async Task<ApiResult<int>> Handle(UpdateRecoveryStatusCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.Ids == null || !request.Ids.Any())
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Data = 0,
                        Msg = "未提供要修改的记录ID"
                    };
                }

                int successCount = 0;
                List<InstrumentRecovery> updatedItems = new List<InstrumentRecovery>();
                
                //批量修改
                foreach (var item in request.Ids)
                {
                    // 根据ID查找登记回收记录
                    var recoveryItem = instrumentRecoveryRep.GetAll().FirstOrDefault(x => x.Id==item);
                    if (recoveryItem == null)
                    {
                        continue; // 跳过不存在的记录，继续处理下一个
                    }
                    
                    // 更新回收状态为已回收
                    recoveryItem.Status = RecoveryStatusEnum.已回收;
                    recoveryItem.RecoveryTime = DateTime.Now;
                    
                    // 添加到待更新列表
                    updatedItems.Add(recoveryItem);
                    successCount++;
                }

                // 批量提交所有更改到数据库
                if (updatedItems.Any())
                {
                    instrumentRecoveryRep.UpdateRange(updatedItems);
                }

                if (successCount == 0)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Data = 0,
                        Msg = "未找到指定的回收登记记录"
                    };
                }

                return new ApiResult<int>
                {
                    Code = ApiEnums.Success,
                    Data = successCount,
                    Msg = $"成功修改{successCount}条回收状态记录"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Data = 0,
                    Msg = ex.Message
                };
            }

        }
    }
}
